#include "UGDefaultEventNotifier.h"

namespace ugame {
    UGDefaultEventNotifier::UGDefaultEventNotifier() {
        this->eventList = CCArray::createWithCapacity(100); // khoi tao list cac event
        CC_SAFE_RETAIN(eventList);
        this->eventHandlerList = CCArray::createWithCapacity(100);//khoi tao list cac handler
        CC_SAFE_RETAIN(eventHandlerList);
    }
    
    UGDefaultEventNotifier::~UGDefaultEventNotifier() {
        CC_SAFE_RELEASE(eventList);
        CC_SAFE_RELEASE(eventHandlerList);
    }
    
    void UGDefaultEventNotifier::registerHandler(UGEventHandler* handler) {
        if (handler != NULL) {
            this->eventHandlerList->addObject(handler); //dang ky handler
            this->sort();
        }
    }
    //notify 1 event cu the
    void UGDefaultEventNotifier::notifyEvent(UGEvent* event) {
        if (event == NULL) {
            return;
        }
        
        if (event->getDelayTime() == 0.0f) {
            this->notify(event);
//            event->release();
        } else {
            for(unsigned int i = 0; i < this->eventList->count(); i++) {
                UGEvent* eventTemp = dynamic_cast<UGEvent*>(this->eventList->getObjectAtIndex(i));
                if (eventTemp != NULL && eventTemp->getId() == event->getId()) {
                    return;
                }
            }
            this->eventList->addObject(event);
        }
    }
    
    void UGDefaultEventNotifier::update(float dt) {
        for(unsigned int i = 0; i < eventList->count(); i++) {
            UGEvent* event = dynamic_cast<UGEvent*>(eventList->getObjectAtIndex(i));
            //event == NULL xoa
            if (event == NULL) {
                this->eventList->removeObjectAtIndex(i);
                i--;
                continue;
            }
            //xu ly nhung event co delay
            float delay = event->getDelayTime();
            delay = delay - dt > 0 ? delay - dt : 0.0f;
            event->setDelayTime(delay);
            
            if (delay > 0) {
                continue;
            }
            //notify event, xu ly xong=>xoa
            this->notify(event);
            this->eventList->removeObjectAtIndex(i);
            i--;
        }
    }
    //duyet tat ca cac handler da duoc dang ky trong handler list cua notifier, neu handler nao bat buoc phai chay 1 cach tu dong ( khong can phai khop voi event(eventID =-1 va eventType=-1) thi chay, handler nao trung khop voi eventID thi chay, handler nao trung khop voi event type thi chay)
    
    void UGDefaultEventNotifier::notify(UGEvent *event) {
        for(unsigned int i = 0; i < this->eventHandlerList->count(); i++) {
            UGEventHandler* handler = dynamic_cast<UGEventHandler*>(this->eventHandlerList->getObjectAtIndex(i));
            
            Ref* handlerObj = handler->getHandlerObj();
            UGHandlerFunc handlerFunc = handler->getHandlerFunc();
            
            if (handler->getEventId() == -1 && handler->getEventType() == -1) {
                (handlerObj->*handlerFunc)(event);
                continue;
            }
            
            if (event->getId() == handler->getEventId()) {
                (handlerObj->*handlerFunc)(event);
                continue;
            }
            
            if (event->getType() == handler->getEventType()) {
                (handlerObj->*handlerFunc)(event);
                continue;
            }
        }
    }
    
    void UGDefaultEventNotifier::sort() {
    }
}
